<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
Yii::import('zii.widgets.jui.CJuiInputWidget');

/**
 * Description of Button
 *
 * @author Chris Sim
 */
class Button extends CJuiInputWidget {

    //put your code here
    /**
     * @var string The button type (possible types: submit, button, link, radio, checkbox).
     * For radio and checkbox you could add
     */
    public $buttonType = 'button';
    /**
     * @var string The url used when a buttonType "link" is selected.
     */
    public $url = null;
    /**
     * @var mixed The value of the current item. Used only for "submit", "button", "radio" and "checkbox"
     */
    public $value;
    /**
     * @var string The button text
     */
    public $caption = "";

    public $CssClass = 'btn';

    public $CssUrl = null;

    public function init() {
        $basePath=Yii::getPathOfAlias('application.widgets.RiotingNerds.assets.button');
        $cssFile = $this->CssUrl;
        $baseUrl=Yii::app()->getAssetManager()->publish($basePath);
        if ($this->CssUrl === null || $this->CssUrl == '') {
            $cssFile = $baseUrl . '/button.css';
        }
        $cs = Yii::app()->getClientScript();
        $cs->registerCssFile($cssFile);
        $this->htmlOptions['class'] .= ' '.$this->CssClass.' ui-corner-all';
    }

    public function run() {
        list($name, $id) = $this->resolveNameID();

        if (!isset($this->htmlOptions['name']))
            $this->htmlOptions['name'] = $name;
        if (!isset($this->htmlOptions['id']))
            $this->htmlOptions['id'] = $id;

        switch ($this->buttonType) {
            case 'submit':
                echo CHtml::submitButton($this->caption, $this->htmlOptions);
                break;
            case 'button':
                $this->renderButton('button', $this->caption, $this->htmlOptions);
                break;
            case 'link':
                $this->renderButton('a', $this->caption, $this->htmlOptions);
                break;
            default:
                throw new CException(Yii::t('zii', 'The button type "{type}" is not supported.', array('{type}' => $this->buttonType)));
        }
    }

    protected function renderButton($type,$caption,$htmlOptions){
        echo CHtml::tag($type, $htmlOptions);
        echo CHtml::tag('span',array(),$caption);
        echo CHtml::closeTag($type);
    }
}

?>
